const Game_Objects = []
export class GameObject {
    constructor() {
        Game_Objects.push(this);
        this.timedelta = 0; //这一帧执行的时刻距离上一帧执行的时刻的时间间隔
        this.has_called_start = false;
    }

    start() { //只执行一次

    }

    update() { //每一帧执行一次,除了第一帧之外 

    }

    before_destory() { //删除之前

    }
    destroy() {
        this.before_destory();
        for (let i in Game_Objects) {
            const obj = Game_Objects[i];
            if (obj === this) {
                Game_Objects.splice(i);
                break;
            }
        }
    }
}

let last_timestamp; //上一次执行的时刻

const step = timestamp => {
    for (let obj of Game_Objects) {
        if (!obj.has_called_start) {
            obj.has_called_start = true;
            obj.start();
        } else {
            obj.timedelta = timestamp - last_timestamp;
            obj.update();
        }
    }

    last_timestamp = timestamp;
    requestAnimationFrame(step)
}

requestAnimationFrame(step)

